#include "cc_print.h"

void cc_printEachChar( 
	char *str 
	)
{
	int temp;
	while(*str)
	{
		temp = (char)*str;
		printf("%x ", temp);
		++str;
	}
}

int cc_simple_printf( 
	const char *format, 
	...
	)
{
	char *arg = (char *)&format + sizeof(format);	// ie, cc_simple_printf("%d", 12), arg means some of the address of number argument 12.
	char ch;
	int printCnt = 0;

	do 
	{
		ch = *format;
		// cc_printf("find char :%c\n", ch);
		if(ch != '%')	// not formatted %, then print it immediately
		{
			putchar(ch);
			++printCnt;
		}
		else			// if it has char '%', then analyze the format, print the right str
		{
			switch(*++format)
			{
			case 'd':	// a decimal integer
				{
					int n = cc_printf("%d", *((int *)arg));
					printCnt += n;
				}
				break;

			case 'x':	// a 16-based integer
				{
					int n = cc_printf("%x", *((int *)arg));
					printCnt += n;
				}
				break;

			default:
				break;
			}
			arg += sizeof(void *);	// the arg address should be updated
		}

		++format;	// analyze the next format char
	} while (*format != '\0');	// loop the format str, to the end

	return printCnt;
}
